<?php
/**
 * Enables you to have a Forum.
 * The forum is lightweight and simple, but is very extendable.
 * 
 * 
 * @author Petter Kjelkenes <kjelkenes@gmail.com>
 * @package Module
 * @subpackage Forum
 * 
 * @ModuleAnnotation(api = 2.0, version = 1.0, author = "Petter Kjelkenes <kjelkenes@gmail.com>")
 * 
 */
class Forum extends Module{
	
	/**
	 * (non-PHPdoc)
	 * @see Module::run()
	 */
	public function run(){
		
		$this->registerLinkMethod(2, 'f', 'forumdisplay');
		$this->registerLinkMethod(2, 'c', 'categorydisplay');
		$this->registerLinkMethod(2, 't', 'threaddisplay');
		
		
		$this->setTPL('tpl/index.tpl');
		
		$categories = $this->getForumMainDisplay();
		$this->template->assign('categories', $categories);
		
	}
	
	public function forumdisplay(){
		$forumid = Url::getBit(3);

		$q = Doctrine_Query::create()
		->select('fm.id, fm.name, fm.description')
		->from('ForumModel fm')
		->where('fm.id = ?', $forumid);		
		
		$forum = $q->execute()->getFirst();

		$threads = $forum->getThreads();
		
		$this->template->assign('threads', $threads);
		
		$this->template->assign('forum', $forum);
		
		$this->setTPL('tpl/forum.tpl');
		
	}
	
	public function threaddisplay(){
		$threadid = Url::getBit(3);

		$q = Doctrine_Query::create()
		->select('ft.id, ft.title,
		tp.id, tp.title, tp.body,
		u.id, u.username,
		')
		->from('ForumThread ft')
		->innerJoin('ft.ThreadPosts tp')
		->innerJoin('ft.ForumModel fm')
		->leftJoin('tp.User u')
		->where('ft.id = ?', $threadid);		
		
		$thread = $q->execute()->getFirst();

		
		
		$this->template->assign('thread', $thread);
		
		$this->setTPL('tpl/thread.tpl');		
	}
	

	
	
	public function categorydisplay(){
		$catid = Url::getBit(3);
		$this->setTPL('frontend/tpl/index.tpl');
		$categories = $this->getForumMainDisplay($catid);
		$this->template->assign('categories', $categories);
		
	}
	
	
	/**
	 * 
	 * Enter description here ...
	 * @param integer $cat_id If you want to display a specific category.
	 */
	
	public function getForumMainDisplay($cat_id=false){
		
		$q = Doctrine_Query::create()
		->select('fc.id, fc.name, fc.description,
		fm.id, fm.name, fm.description,
		ft.id
		')
		->from('ForumCategory fc')
		->leftJoin('fc.ForumModels fm')
		->leftJoin('fm.ForumThreads ft');
		
		if ($cat_id)$q->where('fc.id = ?', $cat_id);
		
		return $q->execute();
		
	}
	
	
	public function install(){
		
		
		
	}
}
